import java.awt.*;
import java.awt.event.*;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import javax.swing.*;
import javax.swing.border.*;
/*
 * Created by JFormDesigner on Mon Nov 29 15:43:14 CST 2021
 */



/**
 * @author 123
 */
public class addCourseDialog extends JDialog {
    Connection conn = null;
    InitialParam initial = null;
    int flag =1;

    public addCourseDialog(Window owner) {
        super(owner);
        initComponents();
        initialConn();
    }


    void initialConn(){
        try {
            initial = new InitialParam();
            conn = initial.conn;
        }catch (Exception e2){
            e2.printStackTrace();
        }
    }

    void modifyCourse(){
        if (Course.isNull()){

        }else{
            cno_textfield.setText(Course.cno);
            pcno_textfield.setText(Course.pcno);
            cname_textfield2.setText(Course.cname);
            score_textfield.setText(Course.score);
            grade_textfield.setText(Course.grade);
        }
    }

    void updateCourse(String cno,String pcno,String cname,String grade,String score) throws SQLException {
        String sql = "update course set cno=?,pcno=?,cname=?,grade=?,score=? where cno = ?";
        if (conn==null){
            JOptionPane.showMessageDialog(addCourseDialog.this,"数据库未连接！","提示",JOptionPane.OK_OPTION);
        }else {
            PreparedStatement st = conn.prepareStatement(sql);
            st.setString(1, cno);
            st.setString(2, pcno);
            st.setString(3, cname);
            st.setString(4, grade);
            st.setString(5, score);
            st.setString(6, cno);
            if (st.execute()) {
                System.out.println("失败");
                JOptionPane.showConfirmDialog(addCourseDialog.this, "修改失败！", "提示", JOptionPane.OK_OPTION);
            } else {
                System.out.println("成功");
                JOptionPane.showConfirmDialog(addCourseDialog.this, "修改成功！", "提示", JOptionPane.CLOSED_OPTION);
            }
        }
    }


    private void okButtonMouseClicked(MouseEvent e) {
        if (flag ==1){
            try{
                addCourse(cno_textfield.getText(),pcno_textfield.getText(),cname_textfield2.getText(),grade_textfield.getText(),score_textfield.getText());
            }catch (Exception e4){
                e4.printStackTrace();
                JOptionPane.showMessageDialog(addCourseDialog.this,"添加失败","提示",JOptionPane.OK_OPTION);
            }
        }else{
            try{
                updateCourse(cno_textfield.getText(),pcno_textfield.getText(),cname_textfield2.getText(),grade_textfield.getText(),score_textfield.getText());
            }catch (Exception e5){
                e5.printStackTrace();
                System.out.println("失败");
                JOptionPane.showConfirmDialog(addCourseDialog.this, "修改失败！", "提示", JOptionPane.OK_OPTION);
            }
        }


    }

    void addCourse(String cno,String PCNO,String CNAME,String GARDE,String SCORE) throws SQLException {
        String sql = "insert into course(cno,pcno,cname,grade,score)" +
                " values (?,?,?,?,?)";
        if (conn==null){
            JOptionPane.showMessageDialog(addCourseDialog.this,"数据库未连接！","提示",JOptionPane.OK_OPTION);
        }else {
            PreparedStatement st = conn.prepareStatement(sql);
            st.setString(1, cno);
            st.setString(2, PCNO);
            st.setString(3, CNAME);
            st.setString(4, GARDE);
            st.setString(5, SCORE);
            if (st.execute()) {
                System.out.println("失败");
                JOptionPane.showConfirmDialog(addCourseDialog.this, "添加失败", "提示", JOptionPane.OK_OPTION);
            } else {
                System.out.println("成功");
                JOptionPane.showConfirmDialog(addCourseDialog.this, "添加成功！", "提示", JOptionPane.CLOSED_OPTION);
            }
        }

    }


    private void cancelButtonMouseClicked(MouseEvent e) {
        this.dispose();
    }

    private void birth_textfieldFocusLost(FocusEvent e) {
        // TODO add your code here
    }

    private void birth_textfieldFocusGained(FocusEvent e) {
        // TODO add your code here
    }

    private void thisWindowActivated(WindowEvent e) {
        //判断是添加用户还是修改用户
        if (!Course.isNull()){
            flag = 2; //修改用户
            modifyCourse();
        }else{
            flag =1;//添加用户
        }
    }

    private void thisWindowClosing(WindowEvent e) {
        Course.putNull();
    }



    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents
        dialogPane = new JPanel();
        contentPanel = new JPanel();
        this2 = new JPanel();
        cno_textfield = new JTextField();
        label1 = new JLabel();
        score_textfield = new JTextField();
        label4 = new JLabel();
        label5 = new JLabel();
        pcno_textfield = new JTextField();
        label6 = new JLabel();
        grade_textfield = new JTextField();
        label7 = new JLabel();
        label3 = new JLabel();
        cname_textfield2 = new JTextField();
        buttonBar = new JPanel();
        okButton = new JButton();
        cancelButton = new JButton();

        //======== this ========
        setTitle("\u8bfe\u7a0b\u4fe1\u606f\u7ba1\u7406\u7cfb\u7edf");
        addWindowListener(new WindowAdapter() {
            @Override
            public void windowActivated(WindowEvent e) {
                thisWindowActivated(e);
            }
            @Override
            public void windowClosing(WindowEvent e) {
                thisWindowClosing(e);
            }
        });
        Container contentPane = getContentPane();
        contentPane.setLayout(new BorderLayout());

        //======== dialogPane ========
        {
            dialogPane.setBorder(new EmptyBorder(12, 12, 12, 12));
            dialogPane.setLayout(new BorderLayout());

            //======== contentPanel ========
            {
                contentPanel.setLayout(null);

                //======== this2 ========
                {
                    this2.setLayout(null);

                    //---- cno_textfield ----
                    cno_textfield.setCursor(Cursor.getPredefinedCursor(Cursor.TEXT_CURSOR));
                    this2.add(cno_textfield);
                    cno_textfield.setBounds(70, 45, 95, cno_textfield.getPreferredSize().height);

                    //---- label1 ----
                    label1.setText("\u8bfe\u7a0b\u53f7:");
                    this2.add(label1);
                    label1.setBounds(20, 55, 50, label1.getPreferredSize().height);

                    //---- score_textfield ----
                    score_textfield.addFocusListener(new FocusAdapter() {
                        @Override
                        public void focusGained(FocusEvent e) {
                            birth_textfieldFocusGained(e);
                        }
                        @Override
                        public void focusLost(FocusEvent e) {
                            birth_textfieldFocusLost(e);
                        }
                    });
                    this2.add(score_textfield);
                    score_textfield.setBounds(70, 120, 95, 30);

                    //---- label4 ----
                    label4.setText("\u5b66\u5206:");
                    this2.add(label4);
                    label4.setBounds(20, 120, 45, 25);

                    //---- label5 ----
                    label5.setText("PCNO:");
                    this2.add(label5);
                    label5.setBounds(185, 50, 50, 17);
                    this2.add(pcno_textfield);
                    pcno_textfield.setBounds(225, 50, 95, 30);

                    //---- label6 ----
                    label6.setText("\u5e74\u7ea7\uff1a");
                    this2.add(label6);
                    label6.setBounds(185, 90, 50, 17);
                    this2.add(grade_textfield);
                    grade_textfield.setBounds(225, 90, 95, 30);

                    //---- label7 ----
                    label7.setText(" ");
                    this2.add(label7);
                    label7.setBounds(new Rectangle(new Point(300, 200), label7.getPreferredSize()));

                    //---- label3 ----
                    label3.setText("\u8bfe\u7a0b\u540d:");
                    this2.add(label3);
                    label3.setBounds(20, 90, 55, 17);
                    this2.add(cname_textfield2);
                    cname_textfield2.setBounds(70, 85, 95, 30);

                    {
                        // compute preferred size
                        Dimension preferredSize = new Dimension();
                        for(int i = 0; i < this2.getComponentCount(); i++) {
                            Rectangle bounds = this2.getComponent(i).getBounds();
                            preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
                            preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
                        }
                        Insets insets = this2.getInsets();
                        preferredSize.width += insets.right;
                        preferredSize.height += insets.bottom;
                        this2.setMinimumSize(preferredSize);
                        this2.setPreferredSize(preferredSize);
                    }
                }
                contentPanel.add(this2);
                this2.setBounds(0, 0, 370, 200);

                {
                    // compute preferred size
                    Dimension preferredSize = new Dimension();
                    for(int i = 0; i < contentPanel.getComponentCount(); i++) {
                        Rectangle bounds = contentPanel.getComponent(i).getBounds();
                        preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
                        preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
                    }
                    Insets insets = contentPanel.getInsets();
                    preferredSize.width += insets.right;
                    preferredSize.height += insets.bottom;
                    contentPanel.setMinimumSize(preferredSize);
                    contentPanel.setPreferredSize(preferredSize);
                }
            }
            dialogPane.add(contentPanel, BorderLayout.CENTER);

            //======== buttonBar ========
            {
                buttonBar.setBorder(new EmptyBorder(12, 0, 0, 0));
                buttonBar.setLayout(new GridBagLayout());
                ((GridBagLayout)buttonBar.getLayout()).columnWidths = new int[] {0, 85, 80};
                ((GridBagLayout)buttonBar.getLayout()).columnWeights = new double[] {1.0, 0.0, 0.0};

                //---- okButton ----
                okButton.setText("\u63d0\u4ea4");
                okButton.addMouseListener(new MouseAdapter() {
                    @Override
                    public void mouseClicked(MouseEvent e) {
                        okButtonMouseClicked(e);
                    }
                });
                buttonBar.add(okButton, new GridBagConstraints(1, 0, 1, 1, 0.0, 0.0,
                    GridBagConstraints.CENTER, GridBagConstraints.BOTH,
                    new Insets(0, 0, 0, 5), 0, 0));

                //---- cancelButton ----
                cancelButton.setText("\u5173\u95ed");
                cancelButton.addMouseListener(new MouseAdapter() {
                    @Override
                    public void mouseClicked(MouseEvent e) {
                        cancelButtonMouseClicked(e);
                    }
                });
                buttonBar.add(cancelButton, new GridBagConstraints(2, 0, 1, 1, 0.0, 0.0,
                    GridBagConstraints.CENTER, GridBagConstraints.BOTH,
                    new Insets(0, 0, 0, 0), 0, 0));
            }
            dialogPane.add(buttonBar, BorderLayout.SOUTH);
        }
        contentPane.add(dialogPane, BorderLayout.CENTER);
        pack();
        setLocationRelativeTo(getOwner());
        // JFormDesigner - End of component initialization  //GEN-END:initComponents
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables
    private JPanel dialogPane;
    private JPanel contentPanel;
    private JPanel this2;
    private JTextField cno_textfield;
    private JLabel label1;
    private JTextField score_textfield;
    private JLabel label4;
    private JLabel label5;
    private JTextField pcno_textfield;
    private JLabel label6;
    private JTextField grade_textfield;
    private JLabel label7;
    private JLabel label3;
    private JTextField cname_textfield2;
    private JPanel buttonBar;
    private JButton okButton;
    private JButton cancelButton;
    // JFormDesigner - End of variables declaration  //GEN-END:variables
}
